package com.lp.business.dao;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.time.DateFormatUtils;
import org.iframework.commons.domain.order.Order;
import org.iframework.commons.domain.pager.Pager;
import org.iframework.commons.util.fast.V;
import org.iframework.support.spring.hibernate.dao.BaseHibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.lp.business.model.Download;

/**
 * 下载信息模型数据访问层
 * 
 * @author lp
 * @email lp@qq.com
 * @date 2018-08-23 10:38:18
 */
@Repository("downloadDao")
public class DownloadDao extends BaseHibernateDaoSupport<Download, String> {
	@Override
	public List<Download> findByModel(Download model, Order order, Pager pager) {
		model = model == null ? new Download() : model;
		StringBuffer hql = new StringBuffer();
		hql.append("from Download c where 1=1");
		hql.append(V.isNotEmpty(model.getId()) ? " and c.id='" + model.getId() + "' " : "");
		hql.append(V.isNotEmpty(model.getStatus()) ? " and c.status='" + model.getStatus() + "' " : "");
		hql.append(V.isNotEmpty(model.getVersion()) ? " and c.version=" + model.getVersion() + " " : "");
		hql.append(V.isNotEmpty(model.getMemberId()) ? " and c.memberId='" + model.getMemberId() + "' " : "");
		hql.append(V.isNotEmpty(model.getReportId()) ? " and c.reportId='" + model.getReportId() + "' " : "");
		hql.append(V.isNotEmpty(model.getDateStart()) && V.isNotEmpty(model.getDateEnd()) ? " and (c.createTime between '" + DateFormatUtils.format(model.getDateStart(), "yyyy-MM-dd HH:mm:ss") + "' and '" + DateFormatUtils.format(model.getDateEnd(), "yyyy-MM-dd HH:mm:ss") + "') " : "");
		hql.append(order != null ? order.toString() : "");
		return this.findByQueryString(hql.toString(),model, pager);
	}

	public List<Map<String, Object>> findDownloads(Download model, Order order, Pager pager) {
		model = model == null ? new Download() : model;
		StringBuffer hql = new StringBuffer();
		hql.append("select c.id,c.reportId,r.name,r.cover,r.createTime,r.industries ");
		hql.append(",(select count(1) from look l where l.reportId = r.id) looktimes "); 
		hql.append(" from download c,report r where c.reportId = r.id ");
		hql.append(V.isNotEmpty(model.getId()) ? " and c.id='" + model.getId() + "' " : "");
		hql.append(V.isNotEmpty(model.getStatus()) ? " and c.status='" + model.getStatus() + "' " : "");
		hql.append(V.isNotEmpty(model.getReportId()) ? " and c.reportId='" + model.getReportId() + "' " : "");
		hql.append(V.isNotEmpty(model.getMemberId()) ? " and c.memberId='" + model.getMemberId() + "' " : "");
		hql.append(order != null ? order.toString() : "");
		return this.querySQL(hql.toString(), pager);
	}
	
}